﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PicACGTool.UserUtils.UserUtils.EX
{
    internal class DicHelper
    {
        /// <summary>
        /// 艺术标签
        /// </summary>
        public static string artist = "https://github.com/EhTagTranslation/Database/blob/master/database/artist.md";

        /// <summary>
        /// 女性标签
        /// </summary>
        public static string femaleApi = "https://github.com/EhTagTranslation/Database/blob/master/database/female.md";

        /// <summary>
        /// 角色翻译
        /// </summary>
        public static string character = "https://github.com/EhTagTranslation/Database/blob/master/database/character.md";

        /// <summary>
        ///  群组
        /// </summary>
        public static string group = "https://github.com/EhTagTranslation/Database/blob/master/database/group.md";

        /// <summary>
        /// 语言
        /// </summary>
        public static string language = "https://github.com/EhTagTranslation/Database/blob/master/database/language.md";

        /// <summary>
        /// 男性标签
        /// </summary>
        public static string male = "https://github.com/EhTagTranslation/Database/blob/master/database/male.md";

        /// <summary>
        /// 原作
        /// </summary>
        public static string parody = "https://github.com/EhTagTranslation/Database/blob/master/database/parody.md";

        /// <summary>
        /// 杂项
        /// </summary>
        public static string misc = "https://github.com/EhTagTranslation/Database/blob/master/database/misc.md";

        /// <summary>
        /// 重新分类
        /// </summary>
        public static string reclass = "https://github.com/EhTagTranslation/Database/blob/master/database/reclass.md";

        /// <summary>
        /// 所有API
        /// </summary>
        public static List<string> ALLAPI = new List<string> { artist, femaleApi, male, parody, character, group, language, reclass, misc };

        public static List<string> ALLContent = new List<string>();

        public static void InitData()

        {
            PrintLog.V("开始下载数据。");
            foreach (var item in ALLAPI)
            {
                SaveSingle(item);
            }
            Console.WriteLine("全部下载完成。");
        }

        /// <summary>
        /// 下载保存单个标签
        /// </summary>
        /// <param name="API"></param>
        private static void SaveSingle(string API)
        {
            string SaveFileName = StaticValue.BinPath + FileNameHelper.GetFileNameWithoutExtension(API) + ".xml";
            FileHelper.FileDelete(SaveFileName);
            PrintLog.V($"下载{FileNameHelper.GetFileNameWithoutExtension(API)}");
            string Shtml = WebHelper.HttpPcGet(API);
            foreach (var item in HtmlHelper.GetHtmlValueList(Shtml, "//tbody/tr"))
            {
                string RomaTag = HtmlHelper.GetFirstText(item, "//td[1]");

                string CnTag = HtmlHelper.GetFirstText(item, "//td[2]");

                string Content = HtmlHelper.OutText(HtmlHelper.GetFirstText(item, "//td[3]"));

                if (RomaTag == "" || CnTag == "")
                    continue;
                FileHelper.AppendText(SaveFileName, $"<tag><cntag>{CnTag}</cntag><romatag>{RomaTag}</romatag><content>{Content}</content></tag>");
            }
        }

        /// <summary>
        /// 按需加载字典词库 提高程序效率
        /// </summary>
        /// <returns></returns>
        public static List<string> InitDic()
        {
            if (ALLContent.Count == 0)
            {
                foreach (var item in FloderNameHelper.GetFullFileNameList(StaticValue.BinPath, "*.xml"))
                {
                    ALLContent.Add(FileHelper.ReadAllText(item));
                }
            }
            return ALLContent;
        }

        /// <summary>
        /// 上传数据的结构体
        /// </summary>
        public class DicStruct
        {
            private string cntag = "";
            private string romaTag = "";
            private string content = "";

            public string Cntag { get => cntag; set => cntag = value; }
            public string RomaTag { get => romaTag; set => romaTag = value; }
            public string Content { get => content; set => content = value; }
        }

        public static DicStruct SerchRomaTag(string RomaTag)
        {
            DicStruct DicInfo = new DicStruct();
            foreach (var item in InitDic())
            {
                if (item.IndexOf($"<romatag>{RomaTag}</romatag>") < 0)
                    continue;
                else
                {
                    string Reslust = SerchRomaTag(item, RomaTag);

                    DicInfo.Cntag = HtmlHelper.GetFirstText(Reslust, "//cntag");
                    DicInfo.Content = HtmlHelper.GetFirstText(Reslust, "//content");
                    DicInfo.RomaTag = HtmlHelper.GetFirstText(Reslust, "//romatag");
                    break;
                }
            }
            if (DicInfo.RomaTag == "")
            { DicInfo.RomaTag = RomaTag; DicInfo.Cntag = "暂无翻译"; DicInfo.Content = ""; }
            return DicInfo;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="FileContent"></param>
        /// <param name="RomaTag"></param>
        /// <returns></returns>
        private static string SerchRomaTag(string FileContent, string RomaTag)
        {
            foreach (var item in HtmlHelper.GetHtmlValueList(FileContent, "//tag"))
            {
                if (item.IndexOf($"<romatag>{RomaTag}</romatag>") >= 0)
                    return item;
            }
            return "";
        }
    }
}